package com.example.calendarprovider.adapter;

import android.annotation.SuppressLint;
import android.graphics.Point;
import android.graphics.drawable.StateListDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.calendarprovider.R;
import com.example.calendarprovider.bean.ImageItem;
import com.example.calendarprovider.utils.SizeUtils;

import java.util.ArrayList;
import java.util.List;
@SuppressLint("UseCompatLoadingForDrawables")
public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.InnerHolder> {

    private static final String TAG = "ImageAdapter";
    private List<ImageItem> mImageItems=new ArrayList<>();

    private List<ImageItem> mSelectItem=new ArrayList<>();
    private OnItemSelectChangeListener mItemSelectChangeListener=null;
    private static final int MAX_SELECTED_COUNT=9;
    private int maxSelectCount=MAX_SELECTED_COUNT;
    private int selectSize=0;
    private  int flag=0;
    private boolean flag2=false;

    public List<ImageItem> getSelectItem() {
        return mSelectItem;
    }
    public void setSelectSize(int mSelectSize){
        selectSize=mSelectSize;
    }
    public int getSelectSize(){
        return selectSize;
    }

    public void setSelectItem(List<ImageItem> selectItem) {
        mSelectItem = selectItem;
    }

    @Override
    public InnerHolder onCreateViewHolder(@NonNull  ViewGroup parent, int viewType) {
        Log.d(TAG, "onCreateViewHolder: "+getSelectSize());
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.image_item, parent, false);
        Point point = SizeUtils.getScreenSize(itemView.getContext());
        RecyclerView.LayoutParams layoutParams=new RecyclerView.LayoutParams(point.x/3,point.x/3);
        itemView.setLayoutParams(layoutParams);
        return new InnerHolder(itemView);
    }

    public int getMaxSelectCount() {
        return maxSelectCount;
    }

    public void setMaxSelectCount(int maxSelectCount) {
        this.maxSelectCount = maxSelectCount;
    }

    @Override
    public void onBindViewHolder(@NonNull  ImageAdapter.InnerHolder holder, int position) {
        //绑定数据(通过源码这里的holder.itemView就是上面onCreateViewHolder里的itemView)
        ImageView imageView = holder.itemView.findViewById(R.id.image_iv);
        CheckBox checkBox = holder.itemView.findViewById(R.id.image_check);
        ImageView textView = holder.itemView.findViewById(R.id.cancel2);
        View cover = holder.itemView.findViewById(R.id.image_cover);
        Glide.with(imageView.getContext()).load(mImageItems.get(position).getPath()).into(imageView);
        ImageItem imageItem = mImageItems.get(position);
        //根据数据状态显示内容（由于itemView会复用先对状态进行处理）
        if(mSelectItem.contains(imageItem)){
            //没有选择上，应该上
            //mSelectItem.add(imageItem);
            //修改UI
            checkBox.setChecked(false);
            cover.setVisibility(View.VISIBLE);
            checkBox.setButtonDrawable(holder.itemView.getContext().getDrawable(R.mipmap.pick_select_true));
            textView.setVisibility(View.GONE);
        }else {
            //已经选择上，应该取消选择
          //  mSelectItem.remove(imageItem);
            //修改UI
            checkBox.setChecked(true);
            cover.setVisibility(View.GONE);
            checkBox.setButtonDrawable(holder.itemView.getContext().getDrawable(R.mipmap.pick_select_false));
            textView.setVisibility(View.GONE);

        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                //是否选择上
                //如果选择上就变成取消
                //如果没选择上就选择上
                if(mSelectItem.contains(imageItem)){
                    //已经选择上，应该取消选择
                    mSelectItem.remove(imageItem);
                    //修改UI
                    checkBox.setChecked(true);

                    cover.setVisibility(View.GONE);
                    checkBox.setButtonDrawable(holder.itemView.getContext().getDrawable(R.mipmap.pick_select_false));
                    textView.setVisibility(View.GONE);
                    Log.d(TAG, "取消onClick: "+mSelectItem.size());


                }else {
                    Log.d(TAG, "选择onClick: "+mSelectItem.size());
                    if(mSelectItem.size()>=MAX_SELECTED_COUNT-getSelectSize()){
                        //给个提示
                        Toast.makeText(checkBox.getContext(),"最多可选择"+maxSelectCount+"张图片",Toast.LENGTH_SHORT).show();
                        return;
                    }
                    //没有选择上，应该上
                    mSelectItem.add(imageItem);
                    //修改UI
                    checkBox.setChecked(false);
                    cover.setVisibility(View.VISIBLE);
                   checkBox.setButtonDrawable(holder.itemView.getContext().getDrawable(R.mipmap.pick_select_true));
                    textView.setVisibility(View.GONE);


                }
                if(mItemSelectChangeListener!=null){
                    mItemSelectChangeListener.onItemSelectChange(mSelectItem);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mImageItems.size();
    }

    public void setData(List<ImageItem> imageItems) {
        mImageItems.clear();
       mImageItems.addAll(imageItems);
       notifyDataSetChanged();

    }

    public void realse() {
        mSelectItem.clear();
    }

    public class InnerHolder extends RecyclerView.ViewHolder {
        public InnerHolder(@NonNull View itemView) {
            super(itemView);
        }
    }

    public void setOnItemSelectChangeListener(OnItemSelectChangeListener listener){
        this.mItemSelectChangeListener=listener;

    }
    public interface OnItemSelectChangeListener{
        void onItemSelectChange(List<ImageItem> selectItems);
    }

}
